'use strict';
const fs = require('fs');
const path = require('path');
const webpack = require('webpack');
const glob = require("glob");
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');


function parsePath(strPath){
    if(path.sep == path.win32.sep){
        return strPath.replace(new RegExp('\\'+path.sep,'g'),path.posix.sep);
    }else{
        return strPath;
    }
}

const outputRoot = path.resolve(__dirname, 'dist');
const srcRoot = path.resolve(__dirname, 'src');

var webpackConfig = {
	mode : 'development',
    entry: {},//具体内容由后面编写的脚本填充
    output: {
        path: outputRoot,
        filename: '[name].js'
    },
    devtool:"inline-source-map",
    devServer:{
        contentBase: './dist'
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                loader: ['style-loader', 'css-loader']
            },
            {
            	test: /\.hbs$/,
            	loader: "handlebars-loader?helperDirs[]="+__dirname+"/src/tpl/helpers"
            },
            {
                test: /\.(png|svg|jpg|gif)$/,
                loader: ['file-loader']
            },
            {
                test: /\.(eot|svg|ttf|woff|woff2|otf)$/,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 8192,
                            name: 'fonts/[name].[ext]'
                        }
                    }
                ]
            }
        ]
    },
    plugins: [
        // 用于清空 output 目录
        //new CleanWebpackPlugin([outputRoot]),
        //用来处理JQuery引用和bootstrap对jquery的依赖
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery",
            'window.jQuery': 'jquery'
        })
    ]

}
//读取根目录下的文件
var jsFilter = parsePath(srcRoot+"/js/*.js");
var entryJs = glob.sync(jsFilter);
console.log(jsFilter);
entryJs.forEach(function(fileName){
    console.log(fileName);
    let tmpFileName = fileName.replace(parsePath(srcRoot+path.sep),"");
    let extension = path.extname(tmpFileName);
    let entryName = tmpFileName.substring(0,tmpFileName.lastIndexOf(extension));
    webpackConfig.entry[entryName] = fileName;      
    console.debug(entryName+" : "+fileName);
});

var htmFilter = parsePath(srcRoot+"/*.html");
var htmFiles = glob.sync(htmFilter);
htmFiles.forEach(function(fileName){
    console.log(fileName);
	let htmFile = fileName.replace(parsePath(path.dirname(fileName)+path.sep),"");
	let extension = path.extname(htmFile);
    let name = htmFile.substring(0,htmFile.lastIndexOf(extension));
        
	let entryName = 'js/page_'+name;
	console.log("Html entry:"+entryName);
	webpackConfig.plugins.push(new HtmlWebpackPlugin({
        filename: htmFile,
        template: fileName,
        inject: true,
        chunks: [entryName] //这个设置使得每个 html 只包含 common 以及与自己命名相同的那一个 chunk
    }));
});

//处理静态文件
webpackConfig.plugins.push(new CopyWebpackPlugin([
    { from: srcRoot+"/css" , to: outputRoot+"/css" },
    { from: srcRoot+"/fonts" , to: outputRoot+"/fonts" },
    { from: srcRoot+"/img" , to: outputRoot+"/img" }
]));

module.exports = webpackConfig;